----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 601337 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 240
SPELL.m_sName = "Bladestorm"
SPELL.m_sToolTip =	[[You spin in circles, causing 
					 50% weapon damage in all 
					 directions for 5 yards.]]
SPELL.m_sIcon = "devin/blackbetty/spells/blademaster/bladestorm.png"
SPELL.m_iCoolDown = 20
SPELL.m_iManaCost = 30
SPELL.m_bRequiresTarget = false
SPELL.m_bRecastWhileCasting = false
SPELL.m_iSpellType = SPELLTYPE_BLADEMASTER

---------------------------------------------------------------------------------------

local range = 100
local weapondamage = 10

function SPELL:Cast( pl, target )
	local target = nil
	local weapon = nil
	if(pl:IsPlayer())then
		weapon = pl:GetWeapon("weapon_all")
		target = pl:GetNetworkedEntity("Target")
	else
		target = pl.Target
	end
	local range = self.m_iRange
	local duration = 3
	local soundfade = 1
	
	self.CastSound = CreateSound( pl, "devin/blackbetty/weapons/castingloops/Bladestorm.wav" )
	self.CastSound:Play()
	pl.DoingCast = true
	pl.CastWhileMoving = true
	timer.Simple(duration-soundfade, function()
		self.CastSound:FadeOut(soundfade)
	end)
	timer.Simple(duration, function()
		pl:SetNWBool("Casting", false)
		pl:SetNWBool("Casted", false)
		pl.DoingCast = false
		self.CastSound:Stop()
		pl:StopParticles()
	end)
	net.Start( "SendPlayerCast" )
	local SendTable = {duration, self.m_sName, Color(255, 155, 0, 155)}
	net.WriteTable(SendTable)
	net.Send(pl)
	ParticleEffectAttach( "whirlwind", PATTACH_POINT_FOLLOW, pl, pl:LookupAttachment("anim_attachment_RH"))
	pl:DoAnimationEvent(ACT_DI_ALYX_ZOMBIE_MELEE)
	local whirlRadius = ents.FindInSphere(pl:GetPos(), 240)
	pl:EmitSound("devin/blackbetty/weapons/Swipe.wav", 140, 110)
	for i, s in pairs(whirlRadius)do
		if(s and s:EntIndex() != pl:EntIndex() and s:IsPlayer() or s:IsNPC() or s:GetClass() == "npc_rpgdm")then
			if(s:Health() > 0)then
				local dmginfo = DamageInfo()
					dmginfo:SetDamage(math.Round(math.random(weapon.MinWeaponDamage, weapon.MaxWeaponDamage))) 
					dmginfo:SetDamageType( DMG_GENERIC ) --Bullet damage
					dmginfo:SetAttacker( pl ) --First player found gets credit
					dmginfo:SetDamageForce( Vector( 0, 0, 1000 ) ) --Launch upwards
				s:TakeDamageInfo( dmginfo )
				ParticleEffect( "pummel_hit", s:GetPos() + Vector(0, 0, 46) + (s:GetAngles():Forward()*6), Angle(0,0,0), pl )
				ParticleEffect( "player_blood", s:GetPos() + Vector(0, 0, 46) + (s:GetAngles():Forward()*6), Angle(0,0,0), pl )
			end
			if SERVER then
				s:SetVelocity(((pl:GetPos()- Vector(0, 0, 40)) - s:GetPos())*-3)
			end	
		end
	end
	timer.Create(""..pl:EntIndex().."bladestormanim", 0.8, duration, function()
		pl:DoAnimationEvent(ACT_DI_ALYX_ZOMBIE_MELEE)
	end)
	timer.Create(""..pl:EntIndex().."bladestorm", 0.5, duration*2, function()
		pl:EmitSound("devin/blackbetty/weapons/Swipe.wav", 140, 110)
		local whirlRadius = ents.FindInSphere(pl:GetPos(), 240)
		for i, s in pairs(whirlRadius)do
			if(s and s:EntIndex() != pl:EntIndex() and s:IsPlayer() or s:IsNPC() or s:GetClass() == "npc_rpgdm")then
				if(s:Health() > 0)then
					local dmginfo = DamageInfo()
						dmginfo:SetDamage(math.Round(math.random(weapon.MinWeaponDamage, weapon.MaxWeaponDamage))) 
						dmginfo:SetDamageType( DMG_GENERIC ) --Bullet damage
						dmginfo:SetAttacker( pl ) --First player found gets credit
						dmginfo:SetDamageForce( Vector( 0, 0, 1000 ) ) --Launch upwards
					s:EmitSound("devin/blackbetty/weapons/GougeTarget.wav", 140, 80)
					s:TakeDamageInfo( dmginfo )
					ParticleEffect( "pummel_hit", s:GetPos() + Vector(0, 0, 46) + (s:GetAngles():Forward()*6), Angle(0,0,0), pl )
				end
				if SERVER then
					s:SetVelocity(((pl:GetPos()- Vector(0, 0, 40)) - s:GetPos())*-3)
				end	
			end
		end
	end)
end
	





